/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Purchase.java
 *
 * Created on Dec 12, 2009, 10:06:33 AM
 */

package GUI;

import Business.PurchaseProcess_BS;
import Business.Purchase_BS;
import Business.loadPurchase;
import DAO.Purchase_DAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author anhvtn_b00545
 */
public class Purchase extends javax.swing.JPanel {
    public static int pid = 0;
    /** Creates new form Purchase */
    private PurchaseProcess_BS pcp = PurchaseProcess_BS.getPurchase();
    private loadPurchase load;
    public Purchase() {
        initComponents();
        try {
            load = pcp.viewPurchase();
            tbPurchaseList.setModel(load);
        } catch (Exception e) {
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnFIilterOrder = new javax.swing.JPanel();
        pnOrder = new javax.swing.JPanel();
        lblCreateOrderDate = new javax.swing.JLabel();
        txtTotalPrice1 = new javax.swing.JTextField();
        txtDatePurchase1 = new javax.swing.JTextField();
        lbTo = new javax.swing.JLabel();
        txtDatePurchase2 = new javax.swing.JTextField();
        lblPrice = new javax.swing.JLabel();
        lbTo1 = new javax.swing.JLabel();
        txtTotalPrice2 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        txtNameSupplier = new javax.swing.JTextField();
        btSearch = new javax.swing.JButton();
        btRefresh = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbPurchaseList = new javax.swing.JTable();
        btCreate = new javax.swing.JButton();
        btDelete = new javax.swing.JButton();
        btClose = new javax.swing.JButton();

        setToolTipText("Purchase");

        pnFIilterOrder.setBorder(javax.swing.BorderFactory.createTitledBorder("Filter"));

        pnOrder.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Purchase", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP));

        lblCreateOrderDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCreateOrderDate.setText("Date Purchase : ");

        lbTo.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbTo.setText("To");

        lblPrice.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice.setText("Total Price : ");

        lbTo1.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbTo1.setText("To");

        javax.swing.GroupLayout pnOrderLayout = new javax.swing.GroupLayout(pnOrder);
        pnOrder.setLayout(pnOrderLayout);
        pnOrderLayout.setHorizontalGroup(
            pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnOrderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCreateOrderDate)
                    .addComponent(lblPrice))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtTotalPrice1)
                    .addComponent(txtDatePurchase1, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnOrderLayout.createSequentialGroup()
                        .addComponent(lbTo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDatePurchase2, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
                            .addComponent(txtTotalPrice2, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)))
                    .addComponent(lbTo1))
                .addContainerGap())
        );
        pnOrderLayout.setVerticalGroup(
            pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnOrderLayout.createSequentialGroup()
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnOrderLayout.createSequentialGroup()
                        .addComponent(txtDatePurchase2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTotalPrice2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnOrderLayout.createSequentialGroup()
                        .addComponent(lblCreateOrderDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPrice))
                    .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(lbTo1)
                        .addGroup(pnOrderLayout.createSequentialGroup()
                            .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtDatePurchase1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lbTo))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtTotalPrice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Supplier", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP));

        lblName.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblName.setText("Name : ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(lblName)
                .addGap(24, 24, 24)
                .addComponent(txtNameSupplier, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNameSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Filter.png"))); // NOI18N
        btSearch.setText("Search");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        btRefresh.setText("Refresh");
        btRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnFIilterOrderLayout = new javax.swing.GroupLayout(pnFIilterOrder);
        pnFIilterOrder.setLayout(pnFIilterOrderLayout);
        pnFIilterOrderLayout.setHorizontalGroup(
            pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btSearch)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                        .addComponent(btRefresh)))
                .addContainerGap())
        );
        pnFIilterOrderLayout.setVerticalGroup(
            pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                .addGroup(pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btSearch)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnFIilterOrderLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btRefresh))))
                    .addComponent(pnOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(49, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Purchase List"));

        tbPurchaseList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbPurchaseList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPurchaseListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbPurchaseList);

        btCreate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Add.png"))); // NOI18N
        btCreate.setText("Create");
        btCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCreateActionPerformed(evt);
            }
        });

        btDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Delete-icon.png"))); // NOI18N
        btDelete.setText("Delete");
        btDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteActionPerformed(evt);
            }
        });

        btClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Close-icon.png"))); // NOI18N
        btClose.setText("Close");
        btClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(260, Short.MAX_VALUE)
                .addComponent(btCreate)
                .addGap(18, 18, 18)
                .addComponent(btDelete)
                .addGap(26, 26, 26)
                .addComponent(btClose)
                .addGap(37, 37, 37))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCreate)
                    .addComponent(btClose)
                    .addComponent(btDelete))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnFIilterOrder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnFIilterOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCreateActionPerformed
        // TODO add your handling code here:
        CreatePurchase purchase= new CreatePurchase(null, true);
        purchase.setVisible(true);

    }//GEN-LAST:event_btCreateActionPerformed

    private void btRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRefreshActionPerformed
        // TODO add your handling code here:
        txtDatePurchase1 .setText("");
        txtTotalPrice1.setText("");
        txtDatePurchase2.setText("");
        txtNameSupplier.setText("");
        txtTotalPrice1.setText("");
        txtTotalPrice2.setText("");
    }//GEN-LAST:event_btRefreshActionPerformed

    private void btDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this Purchase?")
                == JOptionPane.YES_OPTION) {
            int row = tbPurchaseList.getSelectedRow();
            Purchase_BS pur = getPurchaseAtRow(row);
            Purchase_DAO.deletPurchase(pur.getPCID());

            ((loadPurchase) tbPurchaseList.getModel()).deletePurchaser(pur);
        }
    }//GEN-LAST:event_btDeleteActionPerformed

    private void tbPurchaseListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPurchaseListMouseClicked

        if (evt.getClickCount() > 1) {
            int row = tbPurchaseList.getSelectedRow();
            Purchase_BS selectedPurchase = getPurchaseAtRow(row);

            PurchaseDetails purchase = new PurchaseDetails(null, true);
            purchase.setPurchase(selectedPurchase);
            purchase.setVisible(true);
           // pid = Integer.valueOf(tbPurchaseList.getValueAt(tbPurchaseList.getSelectedRow(), 0).toString());

            //((PurchaseDetailsTableModel) tbPurchaseList.getModel()).updateCustomer(selectedCustomer);
        }
    }//GEN-LAST:event_tbPurchaseListMouseClicked

    private void btCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCloseActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btCloseActionPerformed

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
        // TODO add your handling code here:
        try {
            Purchase_DAO p = new Purchase_DAO();
            String supName = txtNameSupplier.getText();

            float price1 = (txtTotalPrice1.getText().length() == 0) ? 
                -1 : Float.parseFloat(txtTotalPrice1.getText());
            float price2 = txtTotalPrice2.getText().length() == 0 ?
                -1 : Float.parseFloat(txtTotalPrice2.getText());
            Date date1 = null;
            Date date2 = null;

            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
            if (!txtDatePurchase1.getText().isEmpty())
                date1 = format.parse(txtDatePurchase1.getText());

            if (!txtDatePurchase2.getText().isEmpty())
            date2 = format.parse(txtDatePurchase2.getText());

            List<Purchase_BS> purchases = p.searchPurchase(supName,price1, price2,date1,date2);
            tbPurchaseList.setModel( new loadPurchase(purchases));
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        
        
    }//GEN-LAST:event_btSearchActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btClose;
    private javax.swing.JButton btCreate;
    private javax.swing.JButton btDelete;
    private javax.swing.JButton btRefresh;
    private javax.swing.JButton btSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTo;
    private javax.swing.JLabel lbTo1;
    private javax.swing.JLabel lblCreateOrderDate;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JPanel pnFIilterOrder;
    private javax.swing.JPanel pnOrder;
    private javax.swing.JTable tbPurchaseList;
    private javax.swing.JTextField txtDatePurchase1;
    private javax.swing.JTextField txtDatePurchase2;
    private javax.swing.JTextField txtNameSupplier;
    private javax.swing.JTextField txtTotalPrice1;
    private javax.swing.JTextField txtTotalPrice2;
    // End of variables declaration//GEN-END:variables

    private Purchase_BS getPurchaseAtRow(int row) {
        loadPurchase model = (loadPurchase) tbPurchaseList.getModel();
        return model.getPurchaseBS(row);
    }

}
